package com.weipu.common.facade.impl;

import android.util.Log;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.MessageServiceProvider;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.model.MessageModel;
import com.weipu.common.facade.model.NoticeGroupsModel;
import com.weipu.common.facade.model.Parmars;
import com.weipu.common.okhttp.JsonUtils;
import com.weipu.common.util.Logger;
import com.weipu.common.util.utils.JsonFormat;

public class MessageServiceProviderImpl extends BaseServiceProviderImpl implements MessageServiceProvider {

    @Override
    public String feedBackNotice(long customerID, String token, String noticeID)
            throws DxException {
        Parmars p = new Parmars();
        p.setCustomerID(customerID);
        p.setToken(token);
        p.setNoticeID(noticeID);
        JSONObject data = requestHttpPostData(Constant.FEEDBACKNOTICE_URL, p);
        return data.optString("NoticeID");
    }

    @Override
    public ArrayList<MessageModel> getNotices(long customerID, String token,
                                              int noticeType, int pageIndex, int pageSize) throws DxException {
        ArrayList<MessageModel> messageModels = null;
        JSONObject data = requestHttpGetData(Constant.GETNOTICES_URL + "?CustomerID=" + customerID + "&Token=" + token + "&NoticeType=" + noticeType +
                "&PageIndex=" + pageIndex + "&PageSize=" + pageSize);
        JSONArray noticePushs = data.optJSONArray("NoticePushs");
        if (null != noticePushs && noticePushs.length() > 0) {
            messageModels = new ArrayList<MessageModel>();
            for (int i = 0; i < noticePushs.length(); i++) {
                JSONObject notice = noticePushs.optJSONObject(i);
                JSONObject noticePush = notice.optJSONObject("NoticePush");
                MessageModel messageModel = JsonUtils.fromJson(noticePush.toString(), MessageModel.class);
                messageModel.setRead(notice.optBoolean("IsRead"));
                messageModels.add(messageModel);
            }
        }
        return messageModels;
    }

    /* (non-Javadoc)
     * @see com.weipu.common.facade.MessageServiceProvider#setNoticeRead(int, java.util.ArrayList, java.lang.Long, java.lang.String)
     */
    @Override
    public boolean setNoticeRead(int actionType, ArrayList<String> noticeIDs,
                                 Long customerID, String token) throws DxException {
        Parmars parmars = new Parmars();
        parmars.setCustomerID(customerID);
        parmars.setToken(token);
        parmars.setActionType(actionType);
        parmars.setNoticeIDs(noticeIDs);
        JSONObject data = requestHttpPostData(Constant.SETNOTICEREAD_URL, parmars);
        if (null != data) {
            return data.optBoolean("Success");
        }
        return false;
    }

//    public boolean setNoticeRead(int actionType, String noticeId,
//                                 Long customerID, String token) throws DxException {
//        Parmars parmars = new Parmars();
//        parmars.setCustomerID(customerID);
//        parmars.setToken(token);
//        parmars.setActionType(actionType);
//        parmars.setNoticeID(noticeId);
//        JSONObject data = requestHttpPostData(Constant.SETNOTICEREAD_URL, parmars);
//        if (null != data) {
//            return data.optBoolean("Success");
//        }
//        return false;
//    }
//

    @Override
    public boolean deleteNotices(int actionType, ArrayList<String> noticeIDs,
                                 Long customerID, String token) throws DxException {
        Parmars parmars = new Parmars();
        parmars.setCustomerID(customerID);
        parmars.setToken(token);
        parmars.setActionType(actionType);
        parmars.setNoticeIDs(noticeIDs);
        JSONObject data = requestHttpPostData(Constant.DELETENOTICES_URL, parmars);
        if (null != data) {
            return data.optBoolean("Success");
        }
        return false;
    }

    @Override
    public ArrayList<NoticeGroupsModel> getNoticeTypeList(long customerID,
                                                          String token) throws DxException {
        ArrayList<NoticeGroupsModel> noticeGroupList = null;
        JSONObject data = requestHttpGetData(Constant.GETNOTICETYPELIST_URL + "?CustomerID=" + customerID + "&Token=" + token);
        JSONArray noticeGroups = data.optJSONArray("NoticeGroups");
        if (null != noticeGroups && noticeGroups.length() > 0) {
            noticeGroupList = new ArrayList<NoticeGroupsModel>();
            for (int i = 0; i < noticeGroups.length(); i++) {
                NoticeGroupsModel model = new NoticeGroupsModel();
                JSONObject optJSONObject = noticeGroups.optJSONObject(i);
                model.setNoticeType(optJSONObject.optInt("NoticeType"));
                model.setUnReadCount(optJSONObject.optInt("UnReadCount"));
                JSONObject noticePush = optJSONObject.optJSONObject("NoticePush");
                if (null != noticePush) {
                    model.setNotcie(JsonUtils.fromJson(noticePush.toString(), MessageModel.class));
                }
                noticeGroupList.add(model);
            }
        }
        return noticeGroupList;
    }

    @Override
    public boolean getIsNotRead(long customerID) throws DxException {
        JSONObject data = requestHttpGetData(Constant.GETISNOTREAD_URL + "?CustomerID=" + customerID);
        boolean isNotRead = data.optBoolean("IsNotRead", false);
        return isNotRead;
    }

}
